import requests

# page = input('请输入要爬取多少页：')
# page = int(page) + 1
header = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
# n = 0
# pn = 1

# url = 'https://coolapi.coolcollege.cn/new-exam-api/v2/enterprises/1643792148006572038/users/1977658442848014336/exams/2094377553430188032/submit-info-list?task_type=all&task_id=2094732089499979776&page_number=1&page_size=10&scene=user'
url = 'https://coolapi.coolcollege.cn/new-exam-api/v2/enterprises/1643792148006572038/users/1977658442848014336/exams/2094377553430188032/submit-info-list'
param = {
    'task_type': 'all',
    'task_id': 2094732089499979776,
    'page_number': 1,
    'page_size': 10,
    'scene': 'user'
}

header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'X-Access-Token': '3c2435ca988d498da5f51597c643e4bd'
}
page_text = requests.get(url=url, headers=header, params=param)
# page_text = "{'code': '200000', 'data': {'end_row': 4, 'first_page': 1, 'has_next_page': 'false', 'has_previous_page': 'false', 'is_first_page': 'true', 'is_last_page': 'true', 'last_page': 1, 'list': [{'answer_duration': '70', 'exam_result': 'failed', 'makeup_exam': 'false', 'mark_name': '系统', 'score': 0.0, 'score_rate': '0%', 'score_record_type': '', 'show_record': 'true', 'submit_id': '2095074543109541888', 'submit_time': '1722861888611', 'task_id': '2094732089499979776', 'total_score': 100.0}, {'answer_duration': '53', 'exam_result': 'failed', 'makeup_exam': 'false', 'mark_name': '系统', 'score': 0.0, 'score_rate': '0%', 'score_record_type': '', 'show_record': 'true', 'submit_id': '2095073856564891648', 'submit_time': '1722861752334', 'task_id': '2094732089499979776', 'total_score': 100.0}, {'answer_duration': '6', 'exam_result': 'failed', 'makeup_exam': 'false', 'mark_name': '系统', 'score': 0.0, 'score_rate': '0%', 'score_record_type': '', 'show_record': 'true', 'submit_id': '2095031653759062016', 'submit_time': '1722848499313', 'task_id': '2094732089499979776', 'total_score': 100.0}, {'answer_duration': '3500', 'exam_result': 'failed', 'makeup_exam': 'false', 'mark_name': '系统', 'score': 38.0, 'score_rate': '38%', 'score_record_type': 'highest', 'show_record': 'true', 'submit_id': '2095008578464256000', 'submit_time': '1722847582392', 'task_id': '2094732089499979776', 'total_score': 100.0}], 'navigate_first_page': 1, 'navigate_last_page': 1, 'navigate_pages': 8, 'navigatepage_nums': [1], 'next_page': 0, 'page_num': 1, 'page_size': 10, 'pages': 1, 'pre_page': 0, 'size': 4, 'start_row': 1, 'total': '4'}, 'message': '', 'success': 'true'}"
page_text.encoding = 'utf-8'
# print(page_text.text, end='\n\n')

# page_text = page_text.text

# print(page_text, end='\n\n')

data = page_text.json()

# print(data['data']['list'], end='\n\n')
# 提取并输出 'submit_id'
submit_ids = [item['submit_id'] for item in data['data']['list']]
print(submit_ids)

# 遍历考试
result_distinct = {}
for submit_id in submit_ids:
    url = 'https://coolapi.coolcollege.cn/new-exam-api/v2/enterprises/1643792148006572038/users/1977658442848014336/exams/2094377553430188032/submit-info/2095074543109541888'
    # url += submit_id
    sub_param = {
        'scene': '',
        'task_id': 2094732089499979776
    }
    sub_text = requests.get(url=url, headers=header, params=sub_param)
    sub_text.encoding = 'utf-8'
    # print(sub_text.text, end='\n\n')

    # 题目内容
    sub_data = sub_text.json()

    # 字母标签列表
    labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
              'V', 'W', 'X', 'Y', 'Z']
    # 提取题目和选项
    result = {}
    index = 1
    for question in sub_data['data']['question_list']:
        question_title = question['title']
        options = question['options']

        # 提取选项及标记正确答案
        option_strings = []
        for i, option in enumerate(options):
            # 使用字母标签
            label = labels[i] if i < len(labels) else f'Option{i + 1}'
            option_title = ''
            is_answer = option['is_answer'] == 'true'
            if is_answer:
                option_title += ' (正确)'
            option_strings.append(option_title + f"{label}: {option['title']}")

        # # 构建结果字典
        # result[question_title] = ', '.join(option_strings)
        # 构建去重的列表
        result_distinct[question_title] = ', '.join(option_strings)
    # print('执行了')

# 输出结果
for index, (question, options) in enumerate(result_distinct.items(), 1):
    print(f"{index}.题目: {question}")
    print(f"选项: {options}\n")
